package com.elong.businesstravel.modules.home.ticket.ticketshow;

import java.io.File;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.android.support.jhf.utils.FileUtils;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseFragmentActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.TicketSearchCondition;
import com.elong.businesstravel.entitys.TrainStation;
import com.elong.businesstravel.modules.home.ticket.adapter.TicketPagerAdapter;
import com.elong.businesstravel.protocol.baseresponse.IHttpResponse;
import com.elong.businesstravel.protocol.ticket.GetStationListRequest;
import com.elong.businesstravel.protocol.ticket.GetStationListResponse;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;

/**
 * 火车票列表页
 * 
 * @author lenovo
 * 
 */
public class TicketListActivity extends BaseFragmentActivity {

	private ViewPager mViewPager;
	private TicketPagerAdapter mTicketListAdapter;

	private TextView mPreviousTextView;
	private TextView mNextTextView;
	private TextView mCurrentDateTextView;

	private TicketSearchCondition mTicketSearchCondition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_ticket_list);

		init();
	}

	@Override
	protected void init() {

		if (null == mSavedInstanceState) {
			mTicketSearchCondition = (TicketSearchCondition) getIntent()
					.getSerializableExtra("ticketsearchcondition");
		} else {
			mTicketSearchCondition = (TicketSearchCondition) mSavedInstanceState
					.getSerializable("ticketsearchcondition");
		}

		mCurrentDateTextView = (TextView) findViewById(R.id.currentDateTextView);
		mCurrentDateTextView.setText(mTicketSearchCondition
				.getStartDateByIndex(0, "yyyy.MM.dd"));

		mPreviousTextView = (TextView) findViewById(R.id.previousTextView);

		mNextTextView = (TextView) findViewById(R.id.nextTextView);

		mViewPager = (ViewPager) findViewById(R.id.viewPager);
		mTicketListAdapter = new TicketPagerAdapter(getSupportFragmentManager());
		mViewPager.setAdapter(mTicketListAdapter);

		DebugLog.logI(this.getClass().toString());

		setListener();
		
		setTitleNavBar();
	}
	
	@Override
	protected void setTitleNavBar() {

		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("车票列表");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView
				.setLeftButtonOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						finish();
					}
				});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	@Override
	protected void setListener() {
		mPreviousTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (0 == mViewPager.getCurrentItem()) {
				} else {
					mViewPager.setCurrentItem(mViewPager.getCurrentItem() - 1,
							true);
				}
			}
		});

		mNextTextView.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mTicketListAdapter.getCount() - 1 == mViewPager
						.getCurrentItem()) {

				} else {
					mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1,
							true);
				}
			}
		});

		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				mCurrentDateTextView.setText(mTicketSearchCondition
						.getStartDateByIndex(arg0, "yyyy.MM.dd"));
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});

	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putSerializable("ticketsearchcondition",
				mTicketSearchCondition);
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if (null != savedInstanceState) {
			mTicketSearchCondition = (TicketSearchCondition) savedInstanceState
					.getSerializable("ticketsearchcondition");
		}
	}

	public TicketSearchCondition getTicketSearchCondition() {
		return mTicketSearchCondition;
	}

}
